package com.he1618;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;

import java.time.Instant;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        String token = "J_b9Esev3GqZtbFLiA_bo6rvye7aaxssC8MiO99KtXgQmfd-VZyNKo3LvoMWfIfNiEZyLncr1UObY2YrArlTbg==";
        String bucket = "ada";
        String org = "ada";

        InfluxDBClient client = InfluxDBClientFactory.create("http://192.168.3.108:8086", token.toCharArray());

        String data = "mem,host=host1 used_percent=23.43234543";

        WriteApiBlocking writeApi = client.getWriteApiBlocking();
        writeApi.writeRecord(bucket, org, WritePrecision.NS, data);



        for (int i = 0; i < 100; i++) {
            Point point = Point
                    .measurement("test")
                    .addTag("host", "host1")
                    .addTag("name", "ada")
                    .addTag("test", "123")
                    .addField("used_percent", Math.random()*1000)
                    .addField("ada", Math.random()*1000)
                    .addField("ada1", Math.random()*1000)
                    .addField("ada2", Math.random()*1000)

                    .time(Instant.now(), WritePrecision.NS);
            writeApi.writePoint(bucket, org, point);
        }

        String query = "from(bucket: \"ada\") |> range(start: -1h)";
        List<FluxTable> tables = client.getQueryApi().query(query, org);

        for (FluxTable table : tables) {
            for (FluxRecord record : table.getRecords()) {
                System.out.println(record);
            }
        }


    }
}
